package com.xiguaji.tiktok.v1.ins.favorite.repository.impl;

import com.xiguaji.boot.toolkit.jdbc.JdbcTemplateHelper;
import com.xiguaji.boot.toolkit.jdbc.Page;
import com.xiguaji.tiktok.v1.ins.favorite.dto.*;
import com.xiguaji.tiktok.v1.ins.favorite.repository.InsFavoriteExtRepository;
import com.xiguaji.tiktok.v1.ins.favorite.vo.PageVo;

public class InsFavoriteExtRepositoryImpl implements InsFavoriteExtRepository {

    @Override
    public Page<BloggerDto> findBloggerPage(PageVo pageVo,long userId) {
        String selectSql = "select f.id f_id,b.uid,b.pic_url,b.full_name,b.category_name,b.overall_category_name,b.follower_count,b.username,b.avg_er,b.is_verified,b.avg_like,b.avg_reviews,b.overall_category_name,r.real_follower_count ";
        String sql = " from ins_blogger_main b " +
                "left join ins_favorite f on b.uid = f.target_id " +
                "left join ins_st_blogger_real_following r on b.uid = r.uid  " +
                "where f.user_id = ? and f.type = ? order by f.create_time desc";
        return JdbcTemplateHelper.paginate(BloggerDto.class,pageVo.getPage(),pageVo.getPageSize(),selectSql,sql,userId, FavoriteTypeEnum.INS_BLOGGER.getCode());
    }


    @Override
    public Page<ProductDto> findProductPage(PageVo pageVo,long userId,long startTime,long endTime) {
        String select = "select p.product_name,p.product_id,p.price,p.image,f.id f_id ";
        String from = " from ins_favorite f " +
                "INNER JOIN ins_product p on f.target_id = p.product_id " +
                "where f.user_id = ? and f.type = ? ";

        Page<ProductDto> page = JdbcTemplateHelper.paginate(ProductDto.class,pageVo.getPage(),pageVo.getPageSize(),select,from,userId,FavoriteTypeEnum.INS_PRODUCT.getCode());

        page.getList().forEach( x->{
            ProductDto temp = JdbcTemplateHelper.findFirstEntity(ProductDto.class,"select mp.product_id, count(distinct(m.owner_uid)) influencer,count(*) media_count,IFNULL(SUM(m.like_count+m.comment_count) ,0) engagements " +
                            "from ins_rel_media_product mp " +
                            "left JOIN ins_media m on mp.shortcode = m.shortcode " +
                            "where mp.product_id = ? and mp.taken_time between ? and ?",x.getProductId(),startTime,endTime);
            x.setInfluencer(temp.getInfluencer());
            x.setMediaCount(temp.getMediaCount());
            x.setEngagements(temp.getEngagements());
        });
        return page;
    }

    @Override
    public Page<MediaDto> findMediaPage(PageVo pageVo, long userId){
        String select = "select m.id,m.shortcode,m.caption,m.is_video,m.display_url,m.video_view_count views,m.like_count likes,m.comment_count reviews,m.er,m.taken_time,m.product_type,m.score,m.is_sponsored,f.id f_id ";
        String from = "from ins_favorite f inner JOIN ins_media m on f.target_id = m.id where f.user_id = ? and f.type = ? order by f.create_time desc";
        return JdbcTemplateHelper.paginate(MediaDto.class,pageVo.getPage(),pageVo.getPageSize(),select,from,userId,FavoriteTypeEnum.INS_MEDIA.getCode());
    }

    @Override
    public Page<BrandDto> findBrandPage(PageVo pageVo,long userId){
        String select = "select b.en_name name,b.id,b.logo,b.category_name category,f.id f_id,sb.media_count posts,sb.like_count likes,sb.review_count reviews,sb.tec engagements ";
        String from = " from ins_favorite f " +
                "INNER JOIN common_brand b on f.target_id = b.id left join ins_st_brand sb on b.id = sb.bid " +
                "where f.user_id = ? and f.type = ? order by f.create_time desc";
        return JdbcTemplateHelper.paginate(BrandDto.class,pageVo.getPage(),pageVo.getPageSize(),select,from,userId,FavoriteTypeEnum.INS_BRAND.getCode());
    }
}
